/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts;

import entity.Applicant;
import entity.Vacancy;
import java.util.ArrayList;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.ApplicantModel;
import model.InterviewModel;
import model.VacancyModel;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import utility.DateUtils;

/**
 *
 * @author Admin
 */
public class UserApplyJobAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        InterviewModel iv = new InterviewModel();
        String vid = request.getParameter("vid");
        String aid = request.getParameter("aid");

        ApplicantModel am = new ApplicantModel();
        Applicant a = am.getOneApplicant(aid);
        String status = a.getStatus();

        if (status.equals("Hired") || status.equals("Banned")) {
            request.setAttribute("error", "* You cann't apply this job ! Because your status are hired or banned.");
            return mapping.findForward("notok");
        }
        else {
            if (iv.insertInterview(vid, aid, DateUtils.now(), "Apply")) {
                return mapping.findForward("ok");
            }
        }

        return mapping.findForward("fail");
    }
}
